﻿using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.Environment;
using Orchard.Environment.Extensions.Models;
using Orchard.Taxonomies.Models;
using Orchard.Taxonomies.Services;

namespace IvyExcellence.HierarchicalUrls.Handlers
{
    public class FeatureHandler : IFeatureEventHandler
    {
        private readonly IContentManager _contentManager;
        private readonly ITaxonomyService _taxonomyService;
        private readonly IContentDefinitionManager _contentDefinitionManager;
        
        public FeatureHandler(IContentDefinitionManager contentDefinitionManager, IContentManager contentManager, ITaxonomyService taxonomyService)
        {
            _contentDefinitionManager = contentDefinitionManager;
            _contentManager = contentManager;
            _taxonomyService = taxonomyService;
        }

        public void Installing(Feature feature)
        {
        }

        public void Installed(Feature feature)
        {
        }

        public void Enabling(Feature feature)
        {
        }

        private TermPart CreateTerm(TaxonomyPart taxonomy, string name, TermPart parent = null)
        {
            var term = _taxonomyService.NewTerm(taxonomy);
            term.Name = name;
            term.Container = parent == null ? taxonomy.ContentItem : parent.ContentItem;
            _taxonomyService.ProcessPath(term);
            _contentManager.Create(term, VersionOptions.Published);
            return term;
        }

        public void Enabled(Feature feature)
        {
            if (feature.Descriptor.Id.Equals("IvyExcellence.HierarchicalUrls"))
            {
                var defaultTaxonomy = "Channels";
                if (_taxonomyService.GetTaxonomyByName(defaultTaxonomy) == null)
                {
                    var taxonomy = _contentManager.New<TaxonomyPart>("Taxonomy");
                    taxonomy.Name = defaultTaxonomy;
                    _contentManager.Create(taxonomy, VersionOptions.Published);

                    var site1 = CreateTerm(taxonomy, "Site1");
                    var site2 = CreateTerm(taxonomy, "Site2", site1);
                }

                _contentDefinitionManager.AlterPartDefinition("AutoroutePart", part => part
                    .WithField("Channels", field => field
                        .OfType("TaxonomyField")
                        .WithSetting("TaxonomyFieldSettings.Taxonomy", defaultTaxonomy)
                        .WithSetting("TaxonomyFieldSettings.SingleChoice", "true")
                        .WithSetting("TaxonomyFieldSettings.Required", "false")));
            }
        }

        public void Disabling(Feature feature)
        {
        }

        public void Disabled(Feature feature)
        {
            if (feature.Descriptor.Name.Equals("IvyExcellence.HierarchicalUrls"))
            {
                _contentDefinitionManager.AlterPartDefinition("AutoroutePart", part => part
                    .RemoveField("Channels"));
            }
        }

        public void Uninstalling(Feature feature)
        {
        }

        public void Uninstalled(Feature feature)
        {
        }
    }
}